package com.huiquan.management.service;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.management.dao.AnalysisLabelDao;
import com.huiquan.management.domain.AnalysisLabel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

@Service
public class AnalysisLabelService extends BaseService {

	@Autowired
	private AnalysisLabelDao analysisLabelDao;
	@Autowired  
	private  HttpSession session; 
	public ModelAndView list(String type) {

		Map<String, Object> param = new HashMap<>();
		param.put("type", type);
		List<AnalysisLabel> pageList = analysisLabelDao.retrieveList(param);
		// 额外属性 [体格检查包含默认属性(zs属性),器械检查包含体格检查属性]
		List<AnalysisLabel> otherList = new ArrayList<>();
		if (StringUtils.equalsIgnoreCase(type, "PHYSICAL_PROPERTY")) {
			param.clear();
			param.put("type", "DEFAULT_PROPERTY");
			List<AnalysisLabel> pageList2 = analysisLabelDao.retrieveList(param);
			otherList.addAll(pageList2);
		}
		if (StringUtils.equalsIgnoreCase(type, "IMAGEOLOGY_PROPERTY")) {
			param.clear();
			param.put("type", "DEFAULT_PROPERTY");
			List<AnalysisLabel> pageList2 = analysisLabelDao.retrieveList(param);
			otherList.addAll(pageList2);
			param.clear();
			param.put("type", "PHYSICAL_PROPERTY");
			pageList2 = analysisLabelDao.retrieveList(param);
			otherList.addAll(pageList2);
		}

		Map<String, Object> map = new HashMap<>();
		map.put("list", pageList);
		if (otherList != null && otherList.size() > 0) {
			map.put("otherList", otherList);
		}
		return new ModelAndView("management/index", map);
	}

	public ReturnData update(long id, User user, String type, String value) {
		AnalysisLabel label = new AnalysisLabel();
		label.setId(id);
		switch (type) {
		case "1":
			label.setLabelName(value);
			break;
		case "2":
			label.setLabel(value);
			break;
		case "3":
			label.setEnglishLabel(value);
			break;
		case "4":
			if (StringUtils.isNumericSpace(value)) {
				label.setRank(Integer.parseInt(value));
			}
			break;
		default:
			break;
		}
		analysisLabelDao.update(label);

		return ReturnUtil.success();
	}

	public ReturnData delete(long id) {
		AnalysisLabel label = new AnalysisLabel();
		label.setId(id);
		analysisLabelDao.delete(label);
		return ReturnUtil.success();
	}

	public ReturnData addLabel(String label, String labelName, String rank, String englishLabel, User user,
			String type) {
		if (StringUtils.equalsIgnoreCase("POSITION2", type)) {
			Map<String, Object> param = new HashMap<>();
			param.put("type", type);
			int count = analysisLabelDao.retrieveSize(param);
			if (count > 0) {
				ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
				rc.setDesc("位置2只能有一条记录");
				return ReturnUtil.fail(rc);
			}
		}
		AnalysisLabel labelValue = new AnalysisLabel();
		labelValue.setLabelName(labelName);
		if (StringUtils.isNumericSpace(rank)) {
			labelValue.setRank(Integer.parseInt(rank));
		} else {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc("rank必须为数字");
			return ReturnUtil.fail(rc);
		}
		labelValue.setEnglishLabel(englishLabel);
		labelValue.setType(type);
		labelValue.setLabel(label);
		analysisLabelDao.insert(labelValue);
		return ReturnUtil.success();
	}

	@SuppressWarnings("unchecked")
	public LinkedHashMap<String, String> getPropertyMap(String type) {
		//先从session中拿
		LinkedHashMap<String, String> rtnMap = new LinkedHashMap<String, String>();
		if(session.getAttribute(type) != null){
			rtnMap =(LinkedHashMap<String, String>) session.getAttribute(type);
		}else{
			getDBPropertyMap(type);
		}
		return rtnMap;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<String> getPositionMap(String type) {
		//先从session中拿
		ArrayList<String> rtnMap = new ArrayList<String>();
		if(session.getAttribute(type) != null){
			rtnMap =(ArrayList<String>) session.getAttribute(type);
		}else{
			getDBPositionMap(type);
		}
		return rtnMap;
	}
	public ArrayList<String> getDBPositionMap(String type) {
		Map<String, Object> param = new HashMap<>();
		param.put("type", type);
		List<AnalysisLabel> label = analysisLabelDao.retrieveList(param);
		ArrayList<String> rtnMap = new ArrayList<String>();
		if(StringUtils.equals(type, "POSITION1")){
			for (AnalysisLabel value : label) {
				rtnMap.add(value.getLabel());
			}
		}else{
			if(label != null && label.size()>0){
				int num = Integer.parseInt(label.get(0).getLabel());
				for (int i = 1; i <= num; i++) {
					rtnMap.add(i + "");
				}
			}
		}
		session.setAttribute(type, rtnMap);
		return rtnMap;
	}
	public LinkedHashMap<String, String> getDBPropertyMap(String type) {
		Map<String, Object> param = new HashMap<>();
		param.put("type", type);
		List<AnalysisLabel> label = analysisLabelDao.retrieveList(param);
		if (StringUtils.equals(type, "IMAGEOLOGY_PROPERTY")) {
			param.clear();
			param.put("type", "PHYSICAL_PROPERTY");
			List<AnalysisLabel> label2 = analysisLabelDao.retrieveList(param);
			label.addAll(label2);
			param.clear();
			param.put("type", "DEFAULT_PROPERTY");
			List<AnalysisLabel> label3 = analysisLabelDao.retrieveList(param);
			label.addAll(label3);
		} else if (StringUtils.equals(type, "IMAGEOLOGY_PROPERTY")) {
			param.clear();
			param.put("type", "DEFAULT_PROPERTY");
			List<AnalysisLabel> label2 = analysisLabelDao.retrieveList(param);
			label.addAll(label2);
		}
		LinkedHashMap<String, String> rtnMap = new LinkedHashMap<String, String>();
		for (AnalysisLabel value : label) {
			rtnMap.put(value.getLabel(), value.getLabel()
					+ (StringUtils.isNotBlank(value.getLabelName()) ? "/" + value.getLabelName() : ""));
		}
		session.setAttribute(type, rtnMap);
		return rtnMap;
	}
}
